package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.constant.Constant;
import com.inspur.his.pojo.DeptExt;
import com.inspur.his.pojo.Doc;
import com.inspur.his.pojo.DocExt;
import com.inspur.his.service.DocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Controller
public class DocController {
    @Autowired
    DocService docService;

    @RequestMapping("docListController")
    public String docList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        //获取页面提交来的查询条件
        String realname = request.getParameter("realname");
        String departid = request.getParameter("departid");
        //  点击医生管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        map.put("realname", realname);
        map.put("departid", departid);
        PageHelper.startPage(pn, 5);
        List<DocExt> docList = docService.docList(map);
        PageInfo<DocExt> pageInfo = new PageInfo<DocExt>(docList, docList.size());
        model.addAttribute("doclist", docList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("realname", realname);
        model.addAttribute("departid", departid);
        return "doc/docList.jsp";
    }

    @RequestMapping("docAddController")
    public String docAdd(Doc doc, Model model) throws Exception {
        //参数绑定 user对象中已经绑定了表单中的用户信息(确认sql中插入的所有字段均有值吗？）
        //思考主键的生成方式--多种1 MySQL中的主键自增
        Date day = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyyMMdd");
        String today = fm.format(day);
        doc.setDocid(today + String.valueOf(day.getTime()));
        doc.setCreatedate(day);
        doc.setStatus(Constant.USER_STATUS_ABLE);
        docService.docAdd(doc);
        model.addAttribute("doc", doc);
        model.addAttribute("flag", Constant.USER_STATUS_ABLE);
        model.addAttribute("success", "医生信息保存成功！");
        return "doc/docList.jsp";
    }

    @RequestMapping("modifyDocController")
    public String modifyDoc(String id,Model model) {
        Doc doc = docService.getDoctorById(id);
        model.addAttribute("doc",doc);
        return "/doc/docModify.jsp";
    }
    @RequestMapping("docUpdateController")
    public String docupdate(Doc doc, Model model) {
        docService.updateDoc(doc);
        model.addAttribute("success", "医生信息修改成功");
        model.addAttribute("doc", doc);
        model.addAttribute("flag", "1");
        return "doc/docModify.jsp";
    }
    @RequestMapping("docchangeStatus")
    public String docchangeStatus(HttpServletRequest req, Model md,
                               @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        // 获取页面传递过来的查询条件
        String name = req.getParameter("name");
        String role = req.getParameter("role");
        // 页面查询条件查询后锁定显示在页面上
        md.addAttribute("name", name);
        md.addAttribute("role", role);

        String docid = req.getParameter("docid");
        String sts = req.getParameter("sts");
        HashMap map = new HashMap<String, String>();
        map.put("docid", docid);
        map.put("status", sts);
        docService.docchangeStatus(map);
        return "redirect:docListController";
    }

    @RequestMapping("doctorList")
    @ResponseBody
    public List<DocExt> docInfo(HttpServletRequest req, Model md) {
        HashMap map = new HashMap<String, String>();
        String departid =req.getParameter("departid");
        map.put("departid",departid);
        List<DocExt> docList = docService.docList(map);
        return docList;
    }

}
